package com.ruoyi.safety.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.safety.domain.PointTypeDict;
import com.ruoyi.safety.dto.AbnormalCountDto;
import com.ruoyi.safety.mapper.PointTypeDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.safety.mapper.SafetyPointAbnormalMapper;
import com.ruoyi.safety.domain.SafetyPointAbnormal;
import com.ruoyi.safety.service.ISafetyPointAbnormalService;

/**
 * 测点异常信息数据Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-30
 */
@Service
public class SafetyPointAbnormalServiceImpl extends ServiceImpl<SafetyPointAbnormalMapper, SafetyPointAbnormal> implements ISafetyPointAbnormalService //修改
{
    @Autowired
    private SafetyPointAbnormalMapper safetyPointAbnormalMapper;

    @Autowired
    private PointTypeDictMapper pointTypeDictMapper;

    /**
     * 查询测点异常信息数据
     *
     * @param pointCode 测点异常信息数据主键
     * @return 测点异常信息数据
     */
    @Override
    public SafetyPointAbnormal selectSafetyPointAbnormalByPointCode(String pointCode) {
        return safetyPointAbnormalMapper.selectSafetyPointAbnormalByPointCode(pointCode);
    }

    /**
     * 查询测点异常信息数据列表
     *
     * @param safetyPointAbnormal 测点异常信息数据
     * @return 测点异常信息数据
     */
    @Override
    public List<SafetyPointAbnormal> selectSafetyPointAbnormalList(SafetyPointAbnormal safetyPointAbnormal) {
        List<SafetyPointAbnormal> pointAbnormalList = safetyPointAbnormalMapper.selectSafetyPointAbnormalList(safetyPointAbnormal);
        List<SafetyPointAbnormal> collect = pointAbnormalList.stream().map(ObjInList -> {
            String abnormalType = ObjInList.getAbnormalType();
            if (!"".equals(abnormalType) && abnormalType != null) {
                PointTypeDict pointTypeDict = pointTypeDictMapper.selectPointTypeDictByCode(abnormalType);
                if (Objects.nonNull(pointTypeDict)) {
                    ObjInList.setAbnormalType(pointTypeDict.getDesc());
                }
            }
            return ObjInList;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 新增测点异常信息数据
     *
     * @param safetyPointAbnormal 测点异常信息数据
     * @return 结果
     */
    @Override
    public int insertSafetyPointAbnormal(SafetyPointAbnormal safetyPointAbnormal) {
        safetyPointAbnormal.setCreateTime(DateUtils.getNowDate());
        return safetyPointAbnormalMapper.insertSafetyPointAbnormal(safetyPointAbnormal);
    }

    /**
     * 修改测点异常信息数据
     *
     * @param safetyPointAbnormal 测点异常信息数据
     * @return 结果
     */
    @Override
    public int updateSafetyPointAbnormal(SafetyPointAbnormal safetyPointAbnormal) {
        return safetyPointAbnormalMapper.updateSafetyPointAbnormal(safetyPointAbnormal);
    }

    /**
     * 批量删除测点异常信息数据
     *
     * @param pointCodes 需要删除的测点异常信息数据主键
     * @return 结果
     */
    @Override
    public int deleteSafetyPointAbnormalByPointCodes(String[] pointCodes) {
        return safetyPointAbnormalMapper.deleteSafetyPointAbnormalByPointCodes(pointCodes);
    }

    /**
     * 删除测点异常信息数据信息
     *
     * @param pointCode 测点异常信息数据主键
     * @return 结果
     */
    @Override
    public int deleteSafetyPointAbnormalByPointCode(String pointCode) {
        return safetyPointAbnormalMapper.deleteSafetyPointAbnormalByPointCode(pointCode);
    }

    @Override
    public int getAbnormalTotal(String sensorTypeName, String abnormalType) {
        int result = 0;
        if (StringUtils.isNotEmpty(sensorTypeName) && StringUtils.isNotEmpty(abnormalType)) {
            result = safetyPointAbnormalMapper.getAbnormalTotal(sensorTypeName, abnormalType);
        } else {
            result = 0;
        }
        return result;
    }

    @Override
    public int getSensorDisconnectionTotal() {
        int total = safetyPointAbnormalMapper.getSensorDisconnectionTotal();
        return total;
    }

    @Override
    public List<SafetyPointAbnormal> getFaultTotal() {
        List<SafetyPointAbnormal> list = safetyPointAbnormalMapper.getFaultTotal();
        return list;
    }

    @Override
    public int getDeviceAbnormalTotal(String sensorTypeName) {
        int result = 0;
        if (StringUtils.isNotEmpty(sensorTypeName)) {
            result = safetyPointAbnormalMapper.getDeviceAbnormalTotal(sensorTypeName);
        } else {
            result = 0;
        }
        return result;
    }

    @Override
    public List<String> getWscxValueList() {
        List<AbnormalCountDto> list = safetyPointAbnormalMapper.getWscxValueList();

        if (list == null || list.isEmpty()) {
            //list  为空
            List<AbnormalCountDto> defaultList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                AbnormalCountDto abnormalCountDto = new AbnormalCountDto();
                abnormalCountDto.setAbnormalCount(0);
                defaultList.add(abnormalCountDto);
            }
            List<String> collect = defaultList.stream().map(objInList -> {
                String abnormalCount = String.valueOf(objInList.getAbnormalCount());
                return abnormalCount;
            }).collect(Collectors.toList());
            return collect;
        } else {
            List<AbnormalCountDto> resultList = new ArrayList<>(list);
            while (resultList.size() < 7) {
                AbnormalCountDto abnormalCountDto = new AbnormalCountDto();
                resultList.add(abnormalCountDto);
            }

            List<String> collect = resultList.stream().map(objInList -> {
                String abnormalCount = String.valueOf(objInList.getAbnormalCount());
                return abnormalCount;
            }).collect(Collectors.toList());
            return collect;
        }

    }

    @Override
//    public List<String> getYyhtValueList() {
    public List<AbnormalCountDto> getYyhtValueList() {
        List<AbnormalCountDto> list = safetyPointAbnormalMapper.getYyhtValueList();

        if (list == null || list.isEmpty()) {
            //list  为空
            List<AbnormalCountDto> defaultList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                AbnormalCountDto abnormalCountDto = new AbnormalCountDto();
                abnormalCountDto.setAbnormalCount(0);
                defaultList.add(abnormalCountDto);
            }
//            List<String> collect = defaultList.stream().map(objInList -> {
//                String abnormalCount = String.valueOf(objInList.getAbnormalCount());
//                return abnormalCount;
//            }).collect(Collectors.toList());
//            return collect;
            return defaultList;

        } else {
            List<AbnormalCountDto> resultList = new ArrayList<>(list);
            while (resultList.size() < 7) {
                AbnormalCountDto abnormalCountDto = new AbnormalCountDto();
                abnormalCountDto.setAbnormalCount(0);
                resultList.add(abnormalCountDto);
            }

//            List<String> collect = resultList.stream().map(objInList -> {
//                String abnormalCount = String.valueOf(objInList.getAbnormalCount());
//                return abnormalCount;
//            }).collect(Collectors.toList());
//            return collect;

            return resultList;
        }

    }

    @Override
    public int getOneDayAbnormalTotal() {
        return safetyPointAbnormalMapper.getOneDayAbnormalTotal();
    }

    @Override
    public List<String> getDyDpWscxValueList() {
        List<AbnormalCountDto> list = safetyPointAbnormalMapper.getDyDpWscxValueList();

        if (list == null || list.isEmpty()) {
            //list  为空
            List<AbnormalCountDto> defaultList = new ArrayList<>();
            for (int i = 0; i < 7; i++) {
                AbnormalCountDto abnormalCountDto = new AbnormalCountDto();
                abnormalCountDto.setAbnormalCount(0);
                defaultList.add(abnormalCountDto);
            }
            List<String> collect = defaultList.stream().map(objInList -> {
                String abnormalCount = String.valueOf(objInList.getAbnormalCount());
                return abnormalCount;
            }).collect(Collectors.toList());
            return collect;
        } else {
            List<AbnormalCountDto> resultList = new ArrayList<>(list);
            while (resultList.size() < 7) {
                AbnormalCountDto abnormalCountDto = new AbnormalCountDto();
                resultList.add(abnormalCountDto);
            }

            List<String> collect = resultList.stream().map(objInList -> {
                String abnormalCount = String.valueOf(objInList.getAbnormalCount());
                return abnormalCount;
            }).collect(Collectors.toList());
            return collect;
    }}
}
